//
//  LOLMineHeroViewController.m
//  LOLXXbox
//
//  Created by mac on 16/1/28.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLMineHeroViewController.h"
#import <Masonry.h>
#import "LOLADDViewController.h"
#import "LSZNetWorkTools.h"
#import "LOLMyheroModel.h"
#import "LOLMyheroCell.h"
#import "LOLHerodetaiViewController.h"
#define MYHEROURL(sn,target) [[NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiMyHeroes.php?serverName=%@&target=%@", sn, target] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

@interface LOLMineHeroViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UILabel *userInfoLabel;
@property (nonatomic, strong)UIView *notSetView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSDictionary *userDic;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, strong)UIButton *button;


@end

@implementation LOLMineHeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(notice:) name:@"123" object:nil];
    
    NSNotificationCenter * centers = [NSNotificationCenter defaultCenter];
    [centers addObserver:self selector:@selector(not:) name:@"12345" object:nil];
    
    [self.view addSubview:self.tableView];
    
    NSString *sanboxPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)[0];
    NSString *documentPath=[sanboxPath stringByAppendingPathComponent:@"userInfo.plist"];
    self.userDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
    self.dataSource = [NSMutableArray array];
    [self getdata];

    
   }


- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,100, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[LOLMyheroCell class] forCellReuseIdentifier:@"cell"];
        UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 20)];
        infoLabel.text = @"*胜率为最近90次的胜率";
        infoLabel.font = [UIFont systemFontOfSize:14];
        infoLabel.backgroundColor = [UIColor colorWithRed:0.994 green:1.000 blue:0.957 alpha:0.900];
        [self.view addSubview:infoLabel];
        [self getdata];
    }
    return _tableView;
}

- (void)getdata {
    NSString *sn = self.userDic[@"sn"];
    NSString *name = self.userDic[@"name"];
    NSString *url = [NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiMyHeroes.php?serverName=%@&target=%@",sn ,name];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [LSZNetWorkTools getWithUrl:url parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = [dic objectForKey:@"myHeroes"];
        for (NSDictionary *DIC in array) {
            LOLMyheroModel *model = [[LOLMyheroModel alloc] initWithData:DIC];
            [self.dataSource addObject:model];
            
        }
        NSInteger moneytCount = 0;
        for (LOLMyheroModel *temp in self.dataSource) {
            NSString *moneyStr = [temp.price componentsSeparatedByString:@","][0];
            moneytCount += moneyStr.integerValue;
            
        }
        
        NSString *sanboxPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)[0];
        NSString *documentPath=[sanboxPath stringByAppendingPathComponent:@"userInfo.plist"];
        self.userDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];

        self.userInfoLabel = [[UILabel alloc] init];
        self.userInfoLabel.frame = CGRectMake(10, 0, self.view.frame.size.width, 50);
        self.userInfoLabel.text = [NSString stringWithFormat:@"召唤师(%@)\n您总共有%ld个英雄,价值%ld金币",self.userDic[@"name"],self.dataSource.count,moneytCount];
        self.userInfoLabel.font = [UIFont systemFontOfSize:15];
        self.userInfoLabel.numberOfLines = 2;
        [self.view addSubview:self.userInfoLabel];
        
     [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LOLMyheroCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = _dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(void)notice:(id)sender{
    NSString *sanboxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)[0];
    NSString *documentPath = [sanboxPath stringByAppendingPathComponent:@"userInfo.plist"];
    self.userDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
    [self.dataSource removeAllObjects];
    [_tableView reloadData];
    [self getdata];
}

-(void)not:(id)sender{
    NSString *sanboxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES)[0];
    NSString *documentPath = [sanboxPath stringByAppendingPathComponent:@"userInfo.plist"];
    self.userDic = [NSDictionary dictionaryWithContentsOfFile:documentPath];
    [self.dataSource removeAllObjects];
    [_tableView reloadData];
    [self.userInfoLabel removeFromSuperview];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LOLHerodetaiViewController *heroDetailVC = [[LOLHerodetaiViewController alloc]init];
    LOLHeroModel *model = self.dataSource[indexPath.row];
    heroDetailVC.heroModel = model;
    [self.navigationController pushViewController:heroDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
